#include "drv_rtt_key/drv_rtt_key.h"
#include "drivers/pin.h"

/*********************************************************************************************
* 名称：key_pin_init()
* 功能：KEY引脚初始化
* 参数：无
* 返回：无
* 修改：
* 注释：
*********************************************************************************************/
void key_pin_init(void)
{
#ifdef BOARD_CONFIG_RTT_KEY0_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_KEY0_PIN, BOARD_CONFIG_RTT_KEY0_PIN_MODE);
#endif
#ifdef BOARD_CONFIG_RTT_KEY1_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_KEY1_PIN, BOARD_CONFIG_RTT_KEY1_PIN_MODE);
#endif
#ifdef BOARD_CONFIG_RTT_KEY2_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_KEY2_PIN, BOARD_CONFIG_RTT_KEY2_PIN_MODE);
#endif
#ifdef BOARD_CONFIG_RTT_KEY3_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_KEY3_PIN, BOARD_CONFIG_RTT_KEY3_PIN_MODE);
#endif
}

/*********************************************************************************************
* 名称：key_getState()
* 功能：KEY按键扫描
* 参数：无
* 返回：按键状态,每位表示一个按键状态，1触发，0没有触发
* 修改：
* 注释：
*********************************************************************************************/
unsigned char key_getState(void)
{
  unsigned char keyVal = 0;
#ifdef BOARD_CONFIG_RTT_KEY0_PIN
  if(rt_pin_read(BOARD_CONFIG_RTT_KEY0_PIN) == BOARD_CONFIG_RTT_KEY0_ACTIVATE)
    keyVal |= K0_PREESED;
#endif
#ifdef BOARD_CONFIG_RTT_KEY1_PIN
  if(rt_pin_read(BOARD_CONFIG_RTT_KEY1_PIN) == BOARD_CONFIG_RTT_KEY1_ACTIVATE)
    keyVal |= K1_PREESED;
#endif
#ifdef BOARD_CONFIG_RTT_KEY2_PIN
  if(rt_pin_read(BOARD_CONFIG_RTT_KEY2_PIN) == BOARD_CONFIG_RTT_KEY2_ACTIVATE)
    keyVal |= K2_PREESED;
#endif
#ifdef BOARD_CONFIG_RTT_KEY3_PIN
  if(rt_pin_read(BOARD_CONFIG_RTT_KEY3_PIN) == BOARD_CONFIG_RTT_KEY3_ACTIVATE)
    keyVal |= K3_PREESED;
#endif
  return keyVal;
}

/*********************************************************************************************
* 名称：u8 KEY_Scan(u8 mode)
* 功能：按键处理函数
* 参数：mode: 0,不支持连续按; 1,支持连续按;
* 返回：返回按键值
* 修改：
* 注释：注意此函数有响应优先级,KEY0>KEY1>KEY2>KEY3!!
*********************************************************************************************/
u8 KEY_Scan(u8 mode)
{
  static u8 key_up=1;                                           //按键按松开标志
  if(mode)key_up=1;                                             //支持连按
  if(key_up&&(  BORAD_KEY0==BOARD_CONFIG_RTT_KEY0_ACTIVATE || BORAD_KEY1==BOARD_CONFIG_RTT_KEY1_ACTIVATE || \
                BORAD_KEY2==BOARD_CONFIG_RTT_KEY2_ACTIVATE || BORAD_KEY3==BOARD_CONFIG_RTT_KEY3_ACTIVATE))
  {
    delay_ms(10);                                               //去抖动 
    key_up=0;
    if(BORAD_KEY0==BOARD_CONFIG_RTT_KEY0_ACTIVATE)return K0_PREESED;
    else if(BORAD_KEY1==BOARD_CONFIG_RTT_KEY1_ACTIVATE)return K1_PREESED;
    else if(BORAD_KEY2==BOARD_CONFIG_RTT_KEY2_ACTIVATE)return K2_PREESED;
    else if(BORAD_KEY3==BOARD_CONFIG_RTT_KEY3_ACTIVATE)return K3_PREESED;
  }else if( BORAD_KEY0==(!BOARD_CONFIG_RTT_KEY0_ACTIVATE) && BORAD_KEY1==(!BOARD_CONFIG_RTT_KEY1_ACTIVATE) && \
            BORAD_KEY2==(!BOARD_CONFIG_RTT_KEY2_ACTIVATE) && BORAD_KEY3==(!BOARD_CONFIG_RTT_KEY3_ACTIVATE)) key_up=1;
  return 0;                                                     // 无按键按下
}
